package src.E_Thread;

// 多个线程同时操作同一个对象
// 买火车票的例子

// 发现问题：多个线程操作同一个资源的情况下，线程不安全，数据混乱
public class E03_Ticket implements Runnable{

    // 票数
    private int num = 10;

    @Override
    public void run() {
        while (true){
            // 没票退出
            if (num <= 0) break;
            // 模拟延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 抢到票打印
            System.out.println(Thread.currentThread().getName()+" 抢到了第 "+num--+" 张票");
        }
    }

    public static void main(String[] args) {
        E03_Ticket ticket = new E03_Ticket();
        new Thread(ticket,"梦梦").start();
        new Thread(ticket,"乐乐").start();
        new Thread(ticket,"璐璐").start();
        new Thread(ticket,"谦谦").start();
        new Thread(ticket,"娜娜").start();
        new Thread(ticket,"毅坨").start();
        new Thread(ticket,"黄牛").start();
    }
}
